<?php

/**
 * Primeiro controller chamado pela aplicacao. Toda estrutura do sammyjs
 * é montado neste controller.
 *
 * @author Igor Rocha <igortice@gmail.com>
 * @copyright Copyright (c) 2011, Igor Rocha
 */
class BuscaController extends BaseController
{

    public function init()
    {
        $this->_helper->layout->disableLayout();
        parent::init();
    }
    
    public function indexAction()
    {
        
    }
    
    public function searchAction()
    {
        $this->ajaxNoRender();
        if ($this->isPostAjax())
            die(true);
        else
            die(false);
    }
    
    public function resultadosAction()
    {
        $folders = new Alfresco_Rest_SpacesStore($this->getUrlAlfresco(), $this->getTicket());
        $search = $this->_getParam('id', null);
        $roots = array();
        foreach(Zend_Registry::get('folderview') as $root)
            $roots[] = $root;
        $this->view->documents = $this->_setParentId($folders->searchDocuments($search, $roots));
    }
    
    private function _setParentId(&$array)
    {
        $folder = new Alfresco_Rest_SpacesStore($this->getUrlAlfresco(), $this->getTicket());
        foreach ($array as $document) 
            $document->parentId = $folder->getParent($document->id)->id;
        
        return $array;
    }

}
